﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.IO;

namespace AvaliacaoOnline.Model
{
    public class UploadService
    {
        public static string GetUploadPath(string serverPath, int idCliente)
        {
            string configPath = ConfigurationManager.AppSettings["UploadFolder"].ToString();

            if (!configPath.EndsWith(@"\"))
                configPath += @"\";

            if (!serverPath.EndsWith(@"\"))
                serverPath += @"\";

            string path = serverPath + configPath + idCliente.ToString() + @"\";

            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);

            return path;
        }

        public static string GetUploadPath(string serverPath, int idCliente, int idAvaliacao)
        {
            string configPath = ConfigurationManager.AppSettings["UploadFolder"].ToString();

            if (!configPath.EndsWith(@"\"))
                configPath += @"\";

            if (!serverPath.EndsWith(@"\"))
                serverPath += @"\";

            string path = serverPath + configPath + idCliente.ToString() + @"\" + idAvaliacao.ToString() + @"\";

            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);

            return path;
        }

        public static string GetUploadPath(string serverPath, int idCliente, int idAvaliacao, int idQuestao)
        {
            string configPath = ConfigurationManager.AppSettings["UploadFolder"].ToString();

            if (!configPath.EndsWith(@"\"))
                configPath += @"\";

            if (!serverPath.EndsWith(@"\"))
                serverPath += @"\";

            string path = serverPath + configPath + idCliente.ToString() + @"\"
                + idAvaliacao.ToString() + @"\" + idQuestao.ToString() + @"\";

            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);

            return path;
        }

        public static string GetFileURL(string serverPath, string fileName, int idCliente)
        {
            string configPath = ConfigurationManager.AppSettings["UploadFolder"].ToString();

            if (String.IsNullOrWhiteSpace(fileName))
                return "";

            if (!configPath.EndsWith(@"/"))
                configPath += @"/";

            if (!serverPath.EndsWith(@"/"))
                serverPath += @"/";

            string path = "/" + configPath + idCliente.ToString() + @"/" + fileName;

            return path;
        }

        public static string GetFileURL(string serverPath, string fileName, int idCliente, int idAvaliacao)
        {
            string configPath = ConfigurationManager.AppSettings["UploadFolder"].ToString();

            if (String.IsNullOrWhiteSpace(fileName))
                return "";

            if (!configPath.EndsWith(@"/"))
                configPath += @"/";

            if (!serverPath.EndsWith(@"/"))
                serverPath += @"/";

            string path = "/" + configPath + idCliente.ToString() + @"/" + idAvaliacao.ToString() + @"/" + fileName;

            return path;
        }

        public static string GetFileURL(string serverPath, string fileName, int idCliente, int idAvaliacao, int idQuestao)
        {
            string configPath = ConfigurationManager.AppSettings["UploadFolder"].ToString();

            if (String.IsNullOrWhiteSpace(fileName))
                return "";

            if (!configPath.EndsWith(@"/"))
                configPath += @"/";

            if (!serverPath.EndsWith(@"/"))
                serverPath += @"/";

            string path = "/" + configPath + idCliente.ToString() + @"/"
                + idAvaliacao.ToString() + @"/" + idQuestao.ToString() + @"/" + fileName;

            return path;
        }

        public static string GetHTMLArquivoMidia(string path)
        {
            string extensao = path.Substring(path.LastIndexOf('.'), path.Length - path.LastIndexOf('.'));
            string nome = path.Substring(path.LastIndexOf('/') + 1, path.Length - path.LastIndexOf('.') - 1);

            string extensoesVideo = ConfigurationManager.AppSettings["ExtensoesVideo"];
            string extensoesImagem = ConfigurationManager.AppSettings["ExtensoesImagem"];
            string extensoesFlash = ConfigurationManager.AppSettings["ExtensoesFlash"];
            string caminhoFlashPlayer = ConfigurationManager.AppSettings["CaminhoFlashPlayer"];

            if (extensoesVideo.IndexOf(extensao) >= 0)
            {
                StringBuilder builder = new StringBuilder();
                //builder.AppendLine(@"<div id='divMP" + nome + @"'></div>");
                //builder.AppendLine(@"<script type=""text/javascript"">");
                //builder.AppendLine(@"    jwplayer('divMP" + nome + @"').setup({");
                //builder.AppendLine(@"        'flashplayer': '" + caminhoFlashPlayer + @"',");
                //builder.AppendLine(@"        'id': 'playerID" + nome + @"',");
                //builder.AppendLine(@"        'width': '400',");
                //builder.AppendLine(@"        'height': '220',");
                //builder.AppendLine(@"        'file': '" + path + @"'");
                //builder.AppendLine(@"    });");
                //builder.AppendLine(@"</script>");

                builder.AppendLine(@"<div id='divMP" + nome + @"'></div>");
                builder.AppendLine(@"<script type=""text/javascript"">");
                builder.AppendLine(@"jwplayer('divMP" + nome + @"').setup({ ");
                builder.AppendLine(@"    'flashplayer': '" + caminhoFlashPlayer + @"', ");
                builder.AppendLine(@"    'file': '" + path + @"', ");
                builder.AppendLine(@"    'controlbar': 'bottom', ");
                builder.AppendLine(@"    'width': '470', ");
                builder.AppendLine(@"    'height': '320' ");
                builder.AppendLine(@"  }); ");
                builder.AppendLine(@"</script>");

                return builder.ToString();
            }
            else if (extensoesFlash.IndexOf(extensao) >= 0)
            {
                StringBuilder builder = new StringBuilder();
                builder.AppendLine(@"<object classid=""clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"" width=""400"" height=""220"" id=""movie" + nome + @""" align=""middle"">");
                builder.AppendLine(@"    <param name=""movie"" value=""" + path + @"""/>");
                builder.AppendLine(@"    <!--[if !IE]>-->");
                builder.AppendLine(@"    <object type=""application/x-shockwave-flash"" data=""" + path + @""" width=""400"" height=""220"">");
                builder.AppendLine(@"        <param name=""movie"" value=""" + path + @"""/>");
                builder.AppendLine(@"    <!--<![endif]-->");
                builder.AppendLine(@"        <a href=""http://www.adobe.com/go/getflash"">");
                builder.AppendLine(@"            <img src=""http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif"" alt=""Get Adobe Flash player""/>");
                builder.AppendLine(@"        </a>");
                builder.AppendLine(@"    <!--[if !IE]>-->");
                builder.AppendLine(@"    </object>");
                builder.AppendLine(@"    <!--<![endif]-->");
                builder.AppendLine(@"</object>");
                return builder.ToString();
            }
            else if (extensoesImagem.IndexOf(extensao) >= 0)
            {
                return String.Format(@"<img src=""{0}""/>", path);
            }

            return "Extensão do arquivo não é conhecida.";
        }
    }
}
